package com.hro.hro_app.main;

import java.text.SimpleDateFormat;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;

import com.hro.hro_app.R;
import com.hro.hro_app.dataController.LessonController;
import com.hro.hro_app.model.Lesson;

public class MainActivity extends Activity implements RoosterDataChanged {

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	@SuppressLint("SimpleDateFormat")
	@Override
	protected void onResume() {

		super.onResume();

		TextView t = (TextView) findViewById(R.id.valnextlesson);

		if (LessonController.LessonCount() > 0) {
			showNextLesson();
		}
		else {
			t.setText("Geen data.");
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage("Rooster niet ingeladen. Inladen?").setPositiveButton("Ja", dialogClickListener).setNegativeButton("Nee", dialogClickListener).show();
		}
	}

	public void showNextLesson() {

		TextView t = (TextView) findViewById(R.id.valnextlesson);
		// Layout
		t.setPadding(15, 15, 15, 15);
		t.setTextSize(18);
		t.setTextColor(Color.WHITE);
		t.setBackgroundColor(Color.argb(255, 204, 0, 51));
		// Einde layout
		Lesson nextlesson = LessonController.getNextlesson();
		if (nextlesson != null) {

			SimpleDateFormat sdfTime = new SimpleDateFormat("HH:mm");
			SimpleDateFormat sdfDate = new SimpleDateFormat("dd-MM");

			String date = sdfDate.format(nextlesson.getStart());
			String end = sdfTime.format(nextlesson.getEnd());
			String from = sdfTime.format(nextlesson.getStart());

			String lessonstring;
			lessonstring = nextlesson.getModule() + '\n' + nextlesson.getTeacher() + " " + nextlesson.getLocation() + '\n' + date + " " + from + "-" + end;
			t.setText(lessonstring);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		MenuInflater menuInflater = getMenuInflater();
		menuInflater.inflate(R.menu.main, menu);

		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {

			case R.id.action_day_view:
				Intent intent = new Intent(this, DayRooster.class);
				startActivity(intent);
				return true;
			case R.id.action_refresh:
				LessonController.refreshData(this);
				return true;
		}

		return super.onOptionsItemSelected(item);
	}

	@Override
	public void roosterDataChanged() {

		// Rooster is geladen/vernieuwd.
		showNextLesson();
	}

	DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {

		@Override
		public void onClick(DialogInterface dialog, int which) {

			switch (which) {
				case DialogInterface.BUTTON_POSITIVE:
					LessonController.refreshData(MainActivity.this);
					break;
				case DialogInterface.BUTTON_NEGATIVE:
					// No button clicked
					break;
			}
		}
	};
}